﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace hello_accinv_New_
{
    public partial class AddStockForm : Form
    {
        private Database db;
        public string loginUser;

        public AddStockForm()
        {
            InitializeComponent();
            db = new Database();
        }

        private void btnSubmitBuy_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Confirm?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                // Do add to stock
                DataGridViewRow dr = datagridBuyProduct.SelectedRows[0];
                db.Insert("Purchase", "0,'" + dr.Cells["BuyPCode"].Value + "','" + CustomDateTime.GetCurrentDate() + "','" + CustomDateTime.GetCurrentTime() + "','" + txtboxBuyQuantity.Text + "','" + txtboxBuyRemark.Text + "'");
                db.Insert("ActionLog", "'" + CustomDateTime.GetCurrentDate() + "','" + CustomDateTime.GetCurrentTime() + "','" + loginUser + " added " + txtboxBuyQuantity.Text + " units " + dr.Cells["BuyPName"].Value + " to stock.'");
                db.Update("Product", "Quantity = Quantity + " + txtboxBuyQuantity.Text, "PCode = '" + dr.Cells["BuyPCode"].Value + "'");
                
                MessageBox.Show("Added to stock", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                cmbBuyCategory.SelectedIndex = -1;
                ClearTableBuyProduct();
            }
        }

        private void ClearTableBuyProduct()
        {
            int rowCount = datagridBuyProduct.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                datagridBuyProduct.Rows.RemoveAt(0);
            }
            lbBuySelectedProduct.ForeColor = Color.Red;
            lbBuySelectedProduct.Text = "No product selected";
            lbBuyProductUnit.Text = "Unit";
            txtboxBuyRemark.Text = "";
            txtboxBuyQuantity.Text = "";
            txtboxBuyQuantity.Enabled = false;
            txtboxBuyRemark.Enabled = false;
        }

        private void btnClearBuyProduct_Click(object sender, EventArgs e)
        {
            cmbBuyCategory.SelectedIndex = -1;
            ClearTableBuyProduct();
        }

        private void cmbBuyCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearTableBuyProduct();
            if (cmbBuyCategory.SelectedIndex != -1 && !cmbBuyCategory.SelectedItem.ToString().Contains("-"))
            {
                String condition;

                DataTable dt = db.Select("CategoryID", "Category", "Name = '" + cmbBuyCategory.SelectedItem.ToString() + "'");
                condition = "C.CategoryID = '" + dt.Rows[0]["CategoryID"].ToString() + "'";

                LoadTableBuyProduct(condition);
            }
            else
            {
                cmbBuyCategory.SelectedIndex = -1;
            }
        }

        private void AddStock_Load(object sender, EventArgs e)
        {
            lbBuyDate.Text = DateTime.Today.ToLongDateString();
            LoadCategoryList();
        }

        private void LoadTableBuyProduct(string condition)
        {
            datagridBuyProduct.DataSource =
                db.Select("P.PCode AS PCode, P.Name AS PName, P.Quantity AS PQuantity, P.Detail AS PDetail, C.Unit AS PUnit",
                          "Product P INNER JOIN Category C ON P.CategoryID = C.CategoryID",
                          condition);
            datagridBuyProduct.ClearSelection();
        }

        private void datagridBuyProduct_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (datagridBuyProduct.SelectedRows.Count > 0)
            {
                DataGridViewRow dr = datagridBuyProduct.SelectedRows[0];
                lbBuySelectedProduct.ForeColor = Color.Blue;
                lbBuySelectedProduct.Text = "Product Code:  " + dr.Cells["BuyPCode"].Value + "     Name:  " + dr.Cells["BuyPName"].Value + "     Stock Quantity:  " + dr.Cells["BuyPQuantity"].Value + "  " + dr.Cells["BuyPUnit"].Value;
                lbBuyProductUnit.Text = dr.Cells["BuyPUnit"].Value.ToString();
                txtboxBuyQuantity.Enabled = true;
                txtboxBuyQuantity.Focus();
                txtboxBuyRemark.Enabled = true;
            }
        }

        private void mtxtboxBuyQuantity_TextChanged(object sender, EventArgs e)
        {
            if (txtboxBuyQuantity.Text.Length > 0)
            {
                btnSubmitBuy.Enabled = true;
            }
            else
            {
                btnSubmitBuy.Enabled = false;
            }
        }

        private void LoadCategoryList()
        {
            DataTable dt = db.Select("Name", "Category", "1 ORDER BY Name ASC");
            List<string> lst = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lst.Add(dt.Rows[i]["Name"].ToString());
            }
            cmbBuyCategory.DataSource = lst;
            cmbBuyCategory.SelectedIndex = -1;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}
